<?php
class Municipalities extends Controller {

    function Municipalities() {
        parent::Controller();
        session_start();
        $this->load->model('municipalities_model');
        $this->lang->load("front");
    }

    function index() {
        redirect(base_url().$this->lang->lang().'/municipalities/chooseMunicipality', 'refresh');
    }

    function test() {
        return 'teset';
    }

    function chooseMunicipality() {
        if(isset($_SESSION['sendData'])){
            unset($_SESSION['sendData']);
        }
        $this->session->unset_userdata('municipality_id');
        $this->session->unset_userdata('municipality_name');
        $this->load->model('municipalities_model');
        $data['allMunicipalities']=$this->municipalities_model->getAllMunicipalities(1);
        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('header',null,true);
        $content['middleFront'] = $this->load->view('choose_municipality',$data,true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('index_front',$content);
    }

    function saveMunicipality(){
        $this->load->model('municipalities_model');
        if($_SERVER['REQUEST_METHOD']=='POST'){
            if(!$this->input->post('municipalities')){
                redirect(base_url().$this->lang->lang().'/municipalities/chooseMunicipality', 'refresh');
            }
            $this->session->set_userdata('municipality_id', $this->input->post('municipalities'));
            $munModel= $this->municipalities_model->getCurrentMunicipalityModel();
            $this->session->set_userdata('municipality_name', $munModel[0]->municipality_name);
            redirect(base_url().$this->lang->lang().'/representatives/pickRepresentative', 'refresh');
        }else{
            if(!$this->session->userdata('municipality_id')){
                redirect(base_url().$this->lang->lang().'/municipalities/chooseMunicipality', 'refresh');
            }
        }
    }
    
}
